package com.my.hzb.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.LoginUserBean;
import com.sun.mail.util.BASE64EncoderStream;


public class CommonUtil {

	public static boolean isNull(Object obj){
		boolean flag = false;
		return flag;
	}
	public static String getRadon(){
		return "";
	}
	
	public static String getMD5(String str){
		String result = null;
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
			result = byte2hex(md.digest(str.getBytes("utf-8")));
        }catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        }catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return result;
	}
	public static String base64encode(String str){
		try {
			return new String(Base64.encodeBase64(str.getBytes("UTF-8")));  
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	public static String base64decode(String str){
		try {
			String strr = new String(Base64.decodeBase64(str));
			return strr.lastIndexOf("{")>-1?strr.substring(0,strr.lastIndexOf("{")):strr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	private static String byte2hex(byte[] b) {
        StringBuffer buf = new StringBuffer();
        int i;

        for (int offset=0; offset<b.length; offset++) {
            i = b[offset];
            if(i<0)
                i += 256;
            if(i<16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }

        return buf.toString();
    }
	public static String getCookieByName(HttpServletRequest request,String cookieName){
		if(null==cookieName){
			return null;
		}
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie coo:cookies){
				if(cookieName.equals(coo.getName())){
					return coo.getValue();
				}
			}
		}
		return null;
	}
	public static LoginUserBean getLoginUserByCoo(HttpServletRequest request,String cookieName){
		LoginUserBean loginUser = null;
		if(null==cookieName){
			return null;
		}
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie coo:cookies){
				if(cookieName.equals(coo.getName())){
					loginUser = (LoginUserBean)JsonUtil.g.fromJson(unescape(coo.getValue()), LoginUserBean.class);
					break;
				}
			}
		}
		return loginUser;
	}
	public static boolean isInterface(List<String> strList,String url){
		if("/hzb_user/".equals(url)){
			return true;
		}
		if(null!=url&&null!=strList){
			if(url.indexOf(".")>-1){
				url = url.substring(0, url.indexOf("."));
			}
			for(String urll:strList){
				if(urll.endsWith("/")){
					if(url.indexOf(urll)>-1){
						return true;
					}
				}else{
					if(url.endsWith(urll)){
						return true;
					}
				}
			}
		}
		return false;
	}
	public static String escape(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * 6);
        for (i = 0; i < src.length(); i++) {
            j = src.charAt(i);
            if (Character.isDigit(j) || Character.isLowerCase(j)
                    || Character.isUpperCase(j))
                tmp.append(j);
            else if (j < 256) {
                tmp.append("%");
                if (j < 16)
                    tmp.append("0");
                tmp.append(Integer.toString(j, 16));
            } else {
                tmp.append("%u");
                tmp.append(Integer.toString(j, 16));
            }
        }
        return tmp.toString();
    }
	public static String unescape(String src) {
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < src.length()) {
            pos = src.indexOf("%", lastPos);
            if (pos == lastPos) {
                if (src.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(src
                            .substring(pos + 2, pos + 6), 16);
                    tmp.append(ch);
                    lastPos = pos + 6;
                } else {
                    ch = (char) Integer.parseInt(src
                            .substring(pos + 1, pos + 3), 16);
                    tmp.append(ch);
                    lastPos = pos + 3;
                }
            } else {
                if (pos == -1) {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                } else {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return tmp.toString();
    }
	public static void main(String[] args) {
		String str = "Hello World  世界";  
        try{  
            byte[] encodeBase64 = Base64.encodeBase64(str.getBytes("UTF-8"));  
            System.out.println("RESULT: " + new String(encodeBase64));
            byte[] by = Base64.decodeBase64(new String(encodeBase64).getBytes());
            System.out.println(new String(by));
            by = Base64.decodeBase64(new String(encodeBase64));
            System.out.println(new String(by));
        } catch(UnsupportedEncodingException e){
            e.printStackTrace();  
        } 
	}
	
}
